//
//  JDNewSaleListVCExtension.swift
//  DragonWallet
//
//  Created by volantPig on 2025/9/13.
//

import UIKit
import SnapKit

extension JDNewSaleListViewController {
        
    func createHeaderView() -> UIView {
        
        let headerView = UIView()
        // 这高度是包含【会员专区】上下圆角的高度
        headerView.frame = CGRectMake(0, 0, screenWidth, 51)
        headerView.backgroundColor = .clear
        headerView.clipsToBounds = true
        
        let bgImageV = UIImageView()
        headerView.addSubview(bgImageV)
        bgImageV.snp.makeConstraints { make in
            make.leading.trailing.equalTo(headerView)
            make.bottom.equalTo(headerView.snp.top).offset(13)
            make.height.equalTo(100)
        }
        bgImageV.image = UIImage(named: "navigationBar_bg_image")
        
        let memberImageV = UIButton()
        headerView.addSubview(memberImageV)
        memberImageV.snp.makeConstraints { make in
            make.trailing.equalTo(headerView)
            make.top.equalTo(headerView).offset(3)
            make.size.equalTo(CGSizeMake(87, 48))
        }
//        memberImageV.image = UIImage(named: "member_image_new")
        memberImageV.setImage(UIImage(named: "member_image_new"), for: .normal)
        memberImageV.addTarget(self, action: #selector(memberAreaAction), for: .touchUpInside)
        
        let white_image = UIImageView()
        headerView.addSubview(white_image)
        white_image.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(91, 51))
            make.bottom.trailing.equalTo(memberImageV)
        }
        white_image.image = UIImage(named: "member_white_image_new")
        
        let otherWhiteView = UIView()
        headerView.addSubview(otherWhiteView)
        otherWhiteView.snp.makeConstraints { make in
            make.trailing.equalTo(white_image.snp.leading)
            make.top.equalTo(headerView)
            make.bottom.equalTo(headerView)
            make.width.equalTo(30)
        }
        otherWhiteView.backgroundColor = .white
        
        
        let leftBgView = UIView()
        leftBgView.backgroundColor = .white
        leftBgView.makeCorner(8)
        headerView.addSubview(leftBgView)
        leftBgView.snp.makeConstraints { make in
            make.leading.top.bottom.equalTo(headerView)
            make.trailing.equalTo(white_image.snp.leading)
        }
        
//        let countryView = JDCountryTypeView()
        leftBgView.addSubview(self.countryView)
        countryView.snp.makeConstraints { make in
            make.leading.equalTo(leftBgView).offset(10)
            make.top.bottom.trailing.equalTo(leftBgView)
        }
        
        self.countryView.refreshGoodsData = { [weak self] in
            guard let weakSelf = self else {return}
            weakSelf.countrySelectedIndex = weakSelf.countryView.selectedIndex
            weakSelf.getNetData(true)
        }
        
        return headerView
    }
    
    func createShopcartButton() {
        let shopcartButtonContainerView = UIView()
        self.view.addSubview(shopcartButtonContainerView)
        shopcartButtonContainerView.snp.makeConstraints { make in
            make.right.equalTo(self.view).offset(-12)
            make.bottom.equalTo(self.view).offset(-142)
            make.size.equalTo(CGSize(width: 64, height: 64))
        }
        shopcartButtonContainerView.makeCorner(4)
        shopcartButtonContainerView.backgroundColor = .white
        
        let imageV = UIImageView(image: UIImage.init(named: "red_shopcart"))
        shopcartButtonContainerView.addSubview(imageV)
        imageV.snp.makeConstraints { make in
            make.centerX.equalTo(shopcartButtonContainerView)
            make.top.equalTo(shopcartButtonContainerView).offset(8)
            make.size.equalTo(CGSizeMake(30, 30))
        }
        
        let titleLabel = UILabel()
        shopcartButtonContainerView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(imageV.snp.bottom).offset(2)
            make.height.equalTo(17)
            make.centerX.equalTo(shopcartButtonContainerView)
        }
        titleLabel.labelConfig(textAlignment: .center, font: UIFont.systemFont(ofSize: 12, weight: .regular), textColor: color("#333333"), numberOfLines: 1)
        titleLabel.text = "购物车"
        
        let shopcartControl = UIControl(frame: CGRect(x: 0, y: 0, width: 64, height: 64))
        shopcartButtonContainerView.addSubview(shopcartControl)
        shopcartControl.addTarget(self, action: #selector(gotoShopCartVC), for: .touchUpInside)
        
        shopcartButtonContainerView.makeBorder(color("#F5F6F8"), 0.5)
    }
    
    @objc func gotoShopCartVC() {
        
        if !JDTools.checkToken() {
            return
        }
        
        let shopcartVC = JDShopcartViewController()
        self.hidesBottomBarPushViewController(shopcartVC)
    }
    
}


class JDCountryTypeView: UIView {
    
    var selectedIndex = 0
    
    var countryModels: [JDCountryInfoModel?]?
    
    var refreshGoodsData:(()->())?
    
    lazy var mainCollectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
        
        let mainCollection = UICollectionView(frame: .zero, collectionViewLayout: layout)
        mainCollection.backgroundColor = .white
        mainCollection.delegate = self
        mainCollection.dataSource = self
        mainCollection.showsHorizontalScrollIndicator = false
        
        mainCollection.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "myCell")
        
        return mainCollection
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(self.mainCollectionView)
        self.mainCollectionView.snp.makeConstraints { make in
            make.leading.trailing.top.bottom.equalTo(self)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension JDCountryTypeView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.countryModels?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCell", for: indexPath)
        
        for subV in cell.contentView.subviews {
            subV.removeFromSuperview()
        }
        
        let currentCountryModel = self.countryModels?[indexPath.row]
        
        let countryImageV = UIImageView()
        cell.contentView.addSubview(countryImageV)
        countryImageV.snp.makeConstraints { make in
            make.centerY.equalTo(cell.contentView)
            make.leading.equalTo(cell.contentView).offset(9)
            make.size.equalTo(CGSizeMake(25, 25))
        }
        countryImageV.makeCorner(12.5)
        countryImageV.kf.setImage(with: URL(string: (currentCountryModel?.countryFlag ?? "")))
        
        let countryNameLabel = UILabel()
        cell.contentView.addSubview(countryNameLabel)
        countryNameLabel.snp.makeConstraints { make in
            make.centerY.equalTo(cell.contentView)
            make.leading.equalTo(countryImageV.snp.trailing).offset(3)
            make.trailing.equalTo(cell.contentView).offset(-9)
        }
//        countryNameLabel.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 13, weight: .semibold), textColor: color("#FA6161"), numberOfLines: 1)
        countryNameLabel.text = currentCountryModel?.countryName
        
        
        if indexPath.row == self.selectedIndex {
            countryNameLabel.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 13, weight: .semibold), textColor: color("#FA6161"), numberOfLines: 1)
        } else {
            countryNameLabel.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 13, weight: .regular), textColor: color("#333333"), numberOfLines: 1)
        }

        
        return cell
    }
    
//    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
////        return CGSizeMake(floor((screenWidth-24.0)/5.0), 90)
//        return UICollectionViewFlowLayout.automaticSize
//    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 12
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0.001
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        self.selectedIndex = indexPath.row
        
        let currentCountryModel = self.countryModels?[indexPath.row]
        print("点击了:\(currentCountryModel?.countryName),code是:\(currentCountryModel?.countryCode)")
        self.refreshGoodsData?()
        
        collectionView.reloadData()
    }
    
}
